import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class StudentGradeManagementSystem {
    private static List<Student> students = new ArrayList<>();
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        showMenu();
    }

    private static void showMenu() {
        while (true) {
            System.out.println("=================================");
            System.out.println("欢迎使用学生成绩管理系统");
            System.out.println("=================================");
            System.out.println("请选择操作：");
            System.out.println("1. 记录学生成绩");
            System.out.println("2. 查询学生成绩");
            System.out.println("3. 统计课程成绩");
            System.out.println("4. 退出系统");
            System.out.print("请输入选项序号：");

            int choice;
            try {
                choice = Integer.parseInt(scanner.nextLine());
            } catch (NumberFormatException e) {
                System.out.println("错误：请输入有效的数字选项！");
                continue;
            }

            switch (choice) {
                case 1:
                    addStudentGrade();
                    break;
                case 2:
                    queryStudentGrade();
                    break;
                case 3:
                    calculateCourseStatistics();
                    break;
                case 4:
                    System.out.println("感谢使用学生成绩管理系统，再见！");
                    return;
                default:
                    System.out.println("错误：无效的选项，请重新输入！");
            }
        }
    }

    private static void addStudentGrade() {
        System.out.println("===== 记录学生成绩 =====");
        System.out.print("请输入学生姓名：");
        String name = scanner.nextLine();

        System.out.print("请输入学生学号：");
        String id = scanner.nextLine();

        // 检查学号是否已存在
        if (isStudentIdExists(id)) {
            System.out.println("错误：该学号已存在！");
            return;
        }

        System.out.print("请输入课程名称：");
        String course = scanner.nextLine();

        System.out.print("请输入成绩（0-100）：");
        double score;
        try {
            score = Double.parseDouble(scanner.nextLine());
            if (score < 0 || score > 100) {
                System.out.println("错误：成绩必须在0-100之间！");
                return;
            }
        } catch (NumberFormatException e) {
            System.out.println("错误：请输入有效的数字成绩！");
            return;
        }

        students.add(new Student(name, id, course, score));
        System.out.println("成绩已成功记录！");
    }

    private static boolean isStudentIdExists(String id) {
        for (Student student : students) {
            if (student.getId().equals(id)) {
                return true;
            }
        }
        return false;
    }

    private static void queryStudentGrade() {
        System.out.println("===== 查询学生成绩 =====");
        System.out.println("请选择查询方式：");
        System.out.println("1. 按学生姓名查询");
        System.out.println("2. 按学生学号查询");
        System.out.println("3. 按课程名称查询");
        System.out.print("请输入选项序号：");

        int choice;
        try {
            choice = Integer.parseInt(scanner.nextLine());
        } catch (NumberFormatException e) {
            System.out.println("错误：请输入有效的数字选项！");
            return;
        }

        switch (choice) {
            case 1:
                queryByName();
                break;
            case 2:
                queryById();
                break;
            case 3:
                queryByCourse();
                break;
            default:
                System.out.println("错误：无效的选项！");
        }
    }

    private static void queryByName() {
        System.out.print("请输入学生姓名：");
        String name = scanner.nextLine();
        boolean found = false;

        for (Student student : students) {
            if (student.getName().equalsIgnoreCase(name)) {
                System.out.println(student);
                found = true;
            }
        }

        if (!found) {
            System.out.println("未找到该学生的成绩记录！");
        }
    }

    private static void queryById() {
        System.out.print("请输入学生学号：");
        String id = scanner.nextLine();
        boolean found = false;

        for (Student student : students) {
            if (student.getId().equals(id)) {
                System.out.println(student);
                found = true;
                break;
            }
        }

        if (!found) {
            System.out.println("未找到该学号的成绩记录！");
        }
    }

    private static void queryByCourse() {
        System.out.print("请输入课程名称：");
        String course = scanner.nextLine();
        boolean found = false;

        for (Student student : students) {
            if (student.getCourse().equalsIgnoreCase(course)) {
                System.out.println(student);
                found = true;
            }
        }

        if (!found) {
            System.out.println("未找到该课程的成绩记录！");
        }
    }

    private static void calculateCourseStatistics() {
        System.out.println("===== 统计课程成绩 =====");
        System.out.print("请输入课程名称：");
        String course = scanner.nextLine();

        List<Double> scores = new ArrayList<>();
        for (Student student : students) {
            if (student.getCourse().equalsIgnoreCase(course)) {
                scores.add(student.getScore());
            }
        }

        if (scores.isEmpty()) {
            System.out.println("未找到该课程的成绩记录！");
            return;
        }

        double sum = 0;
        double max = Double.MIN_VALUE;
        double min = Double.MAX_VALUE;

        for (double score : scores) {
            sum += score;
            if (score > max) max = score;
            if (score < min) min = score;
        }

        double average = sum / scores.size();

        System.out.println("课程：" + course);
        System.out.printf("平均分：%.2f\n", average);
        System.out.println("最高分：" + max);
        System.out.println("最低分：" + min);
    }
}